package com.shelpe.services.sinbad.entity.jpa;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.controller.LogDto.LogNoteable;
import com.shelpe.services.sinbad.service.utils.EntityUtils.DriveTypeConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.OnlineStatusConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.StatusConverter;

import lombok.Data;

@Entity(name = "adgroups")
@Data
public class Adgroup implements LogNoteable{

	@Id
	@Column(name = "adgroup_id")
	@JsonProperty("adgroup_id")
	private long autoID;
	private String nick;
	@Column(name = "user_id")
	@JsonIgnore
	private long userID;
	@Column(name = "campaign_id")
	@JsonProperty("campaign_id")
	private long campaignID;
	@Column(name = "category_ids")
	@JsonProperty("category_ids")
	private String categoryIDs;

	@Column(name = "num_iid")
	@JsonProperty("num_iid")
	private long productID;
	@Column(name = "title")
	@JsonProperty("title")
	private String productName;
	@Column(name = "product_props")
	@JsonIgnore
	private String productAttributes;
	@Column(name = "img_url")
	@JsonProperty("img_url")
	private String productUrl;
	@JsonIgnore
	private String productType;
	@Convert(converter = DriveTypeConverter.class)
	@JsonIgnore
	private DriveType driveType = DriveType.auto;

	@Column(name = "is_nonsearch_max_price")
	@JsonProperty("is_nonsearch_max_price")
	private boolean nonsearch;
	@JsonProperty("default_price")
	private int defaultPrice;
	@JsonIgnore
	private int nonsearchMaxPrice;
	@JsonProperty("mobile_discount")
	private int mobileDiscount = 100;

	@Convert(converter = StatusConverter.class)
	@JsonIgnore
	private Status status;

	@Convert(converter = OnlineStatusConverter.class)
	@JsonProperty("online_status")
	private OnlineStatus onlineStatus;
	@Convert(converter = OnlineStatusConverter.class)
	@JsonProperty("offline_type")
	private OnlineStatus offlineType;
	@JsonIgnore
	private String reason;
	@JsonIgnore
	private String placeId = "[]";
	
	@JsonProperty("modified_time")
	private Date modifiedTime;
	@JsonProperty("create_time")
	private Date createTime;
	@JsonIgnore
	private Date updatedTime;
	@JsonIgnore
	private Date maintainedDate;
	
	private int adjustCount;
	@Column(name = "`limit`")
	private int limit;

	public static enum Status {
		idle(0), updating(1), paused(2), waitingConfirm(-1), blank(-9);

		private int value;

		private Status(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
		
		public static Status from(int value){
			Status en = Status.idle;
			for (Status s : Status.values()) {
				if (s.getValue() == value) {
					en = s;
					break;
				}
			}
			return en;
		}
	}
}
